﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.Linq;
using OnlineStore.Bll;

namespace OnlineStore.Dal
{
    public class DalOrder
    {
        OnlineStoreDataContext data = new OnlineStoreDataContext();
        public DalOrder() { }
        public bool addOrder(List<bizCart> listCart, tbCustomer User, DateTime Reviewed_Date, string Address,bizVoucher voucherItem)
        {
            try
            {
                DalProduct _dalProduct = new DalProduct();
                tbOrder _order = new tbOrder();
                _order.Order_Id = Max + 1;
                _order.Customer_ID = User.Cus_ID;
                _order.Status = "Waiting";
                _order.Created_Date = DateTime.Now;
                _order.Reviewed_Date = Reviewed_Date;
                _order.Address_Order = Address;
                double Total = 0;
                foreach (bizCart i in listCart)
                    Total += i.Sum;
                _order.Total =(int) (bizCart.PriceOrder(Total) - voucherItem.Values);
                _order.Voucher_ID = voucherItem.VoucherItem.Voucher_ID;
                data.tbOrders.InsertOnSubmit(_order);
                foreach (bizCart i in listCart)
                {
                    _dalProduct.UpdateOrder(i);
                    tbOrderDetail _orderDetail = new tbOrderDetail();
                    _orderDetail.Order_ID = _order.Order_Id;
                    _orderDetail.Product_ID = i.ID;
                    _orderDetail.Price = i.PriceProduct;
                    _orderDetail.Qty = i.NumberOrder;
                    data.tbOrderDetails.InsertOnSubmit(_orderDetail);
                }
                data.SubmitChanges();
                return true;
            }
            catch(ChangeConflictException)
            {
                data.ChangeConflicts.ResolveAll(RefreshMode.KeepChanges);
                data.SubmitChanges();
                return false;
            }
        }
        public bool addOrder(List<bizCart> listCart, tbCustomer User, DateTime Reviewed_Date, string Address)
        {
            try
            {
                DalProduct _dalProduct = new DalProduct();
                tbOrder _order = new tbOrder();
                _order.Order_Id = Max + 1;
                _order.Customer_ID = User.Cus_ID;
                _order.Status = "Waiting";
                _order.Created_Date = DateTime.Now;
                _order.Reviewed_Date = Reviewed_Date;
                _order.Address_Order = Address;
                double Total = 0;
                foreach (bizCart i in listCart)
                    Total += i.Sum;
                _order.Total = (int)(bizCart.PriceOrder(Total));
                data.tbOrders.InsertOnSubmit(_order);
                foreach (bizCart i in listCart)
                {
                    _dalProduct.UpdateOrder(i);
                    tbOrderDetail _orderDetail = new tbOrderDetail();
                    _orderDetail.Order_ID = _order.Order_Id;
                    _orderDetail.Product_ID = i.ID;
                    _orderDetail.Price = i.PriceProduct;
                    _orderDetail.Qty = i.NumberOrder;
                    data.tbOrderDetails.InsertOnSubmit(_orderDetail);
                }
                data.SubmitChanges();
                return true;
            }
            catch (ChangeConflictException)
            {
                data.ChangeConflicts.ResolveAll(RefreshMode.KeepChanges);
                data.SubmitChanges();
                return false;
            }
        }
        public static int Max
        {
            get
            {
                OnlineStoreDataContext data = new OnlineStoreDataContext();
                var list = (from p in data.tbOrders
                       select p.Order_Id).ToList();
                if (list.Count== 0 ) return 0;

                return list.Max();
            }

        }
        public List<tbOrder> LoadAllOrder()
        {
            var list = (from p in data.tbOrders
                        orderby p.Order_Id descending
                        select p).ToList();
            return list;
        }
        public tbOrder LoadOrder(int Order_ID)
        {
            try
            {
                var OrderItem = data.tbOrders.Single(p => p.Order_Id == Order_ID);
                return OrderItem;
            }
            catch
            {
                return null;
            }
        }
        public tbCustomer LoadUser(int Order_ID)
        {
            var User = (from p in data.tbOrders
                        from u in data.tbCustomers
                        where p.Customer_ID == u.Cus_ID
                        select u).FirstOrDefault();
            return User;
        }
        public bool Update(tbOrder OrderItem)
        {
            try
            {
                tbOrder item = data.tbOrders.Single(p => p.Order_Id == OrderItem.Order_Id);
                if (item.Status == "Waiting")
                {
                    List<tbOrderDetail> listOrder = LoadOrderDetail(item.Order_Id);
                    DalProduct _dalProduct = new DalProduct();
                    if (listOrder == null) return false;
                    if (item.Status == "Error")
                    {
                        foreach (tbOrderDetail i in listOrder)
                            _dalProduct.UpdateError(i);
                    }
                    else
                    {
                        foreach (tbOrderDetail i in listOrder)
                            _dalProduct.UpdateSuccess(i);
                    }
                }
                item.Status = OrderItem.Status;
                item.Delivery_Date = OrderItem.Delivery_Date;
                item.Address_Order = OrderItem.Address_Order;
                data.SubmitChanges();
                return true;
            }
            catch
            { return false; }
        }
        private List<tbOrderDetail> LoadOrderDetail(int OrderID)
        {
            var listOrderDetail = (from p in data.tbOrderDetails
                                   where p.Order_ID == OrderID
                                   select p).ToList();
            return listOrderDetail;
        }
        public List<bizOrderDetail> LoadOrderDetail(string OrderID)
        {
            int id = Convert.ToInt32(OrderID);
            var listOrderDetail = (from p in data.tbOrderDetails
                                   from product in data.tbProducts
                                   where p.Order_ID == id && p.Product_ID == product.ID
                                   select new bizOrderDetail
                                   {
                                       Product_ID = p.Product_ID,
                                       Order_ID = p.Order_ID,
                                       NumberOrder = Convert.ToInt32(p.Qty),
                                       Picture = product.Picture,
                                       Product_Name = product.Product_Name,
                                       Type = product.Type,
                                       PriceProduct = Convert.ToInt32(p.Price),
                                   }).ToList<bizOrderDetail>();
            return listOrderDetail;
        }

    }
}